package com.carkeeper.search.controller;

import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.pojo.TweetInfo;
import com.carkeeper.search.service.TweetService;
import com.carkeeper.serviceManagement.feign.ServiceTypeFeign;
import com.carkeeper.serviceManagement.pojo.ServiceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tweet")
@CrossOrigin
@Api(tags = "知识库管理接口api",description = "知识库管理管理接口，提供页面的增、删、改、查")

public class TweetController {
    @Autowired
    private TweetService tweetService;
    @Autowired
    private ServiceTypeFeign serviceTypeFeign;

    /**
     * 添加
     *
     * @param tweetInfo
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "add",notes = "添加推文",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "文章名称",required = true,paramType = "body"),
            @ApiImplicitParam(name = "image",value = "标题图片",required = true,paramType = "body"),
            @ApiImplicitParam(name = "category",value = "文章总分类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "category1",value = "一级分类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "category2",value = "二级分类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "content",value = "推文内容",required = true,paramType = "body")
    })
    public Result add(@RequestBody TweetInfo tweetInfo) {
        tweetService.add(tweetInfo);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 查询所有一级分类
     */
    @GetMapping("/findLevel1")
    @ApiOperation(value = "findLevel1",notes = "查询所有一级分类",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category",value = "文章总分类",required = true,paramType = "path"),
    })
    public Result findLevel1(String category) {
        //category总分类为0,则查询所有服务一级分类,为1查询配件分类
        if ("0".equals(category)) {
            Result<List<ServiceType>> result = serviceTypeFeign.findLevel1();
            return result;
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }

    /**
     * 根据一级分类id查询所有二级分类
     */
    @GetMapping("/findLevel2")
    @ApiOperation(value = "findLevel2ListByLevel1Id",notes = "查询所有二级分类",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "一级分类id",required = true,paramType = "path"),
    })
    public Result findLevel2ListByLevel1Id(Long id) {
        Result<List<ServiceType>> result = serviceTypeFeign.findLevel2(id);
        return result;
    }

    //查询
    @GetMapping
    @ApiOperation(value = "search",notes = "查询推文列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "文章名称",required = false,paramType = "query"),
            @ApiImplicitParam(name = "category",value = "文章总分类",required = false,paramType = "query"),
            @ApiImplicitParam(name = "category1",value = "一级分类",required = false,paramType = "query"),
            @ApiImplicitParam(name = "category2",value = "二级分类",required = false,paramType = "query"),
    })
    public Result search(@RequestParam Map map) {
        Map resultMap = tweetService.search(map);
        return new Result(true, StatusCode.OK, "查询成功", resultMap);
    }

    //修改
    @PutMapping
    @ApiOperation(value = "edit",notes = "修改推文",httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "body"),
            @ApiImplicitParam(name = "name",value = "文章名称",required = false,paramType = "body"),
            @ApiImplicitParam(name = "image",value = "标题图片",required = false,paramType = "body"),
            @ApiImplicitParam(name = "category",value = "文章总分类",required = false,paramType = "body"),
            @ApiImplicitParam(name = "category1",value = "一级分类",required = false,paramType = "body"),
            @ApiImplicitParam(name = "category2",value = "二级分类",required = false,paramType = "body"),
            @ApiImplicitParam(name = "content",value = "推文内容",required = false,paramType = "body")
    })
    public Result edit(@RequestBody TweetInfo tweetInfo) {
        tweetService.edit(tweetInfo);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //根据id查询
    @GetMapping("/{id}")
    @ApiOperation(value = "findById",notes = "查询推文详情",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "推文id",required = true,paramType = "path"),
    })
    public Result findById(@PathVariable String id) {
        TweetInfo tweetInfo = tweetService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", tweetInfo);
    }
    //根据id删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "delete",notes = "删除推文",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "推文id",required = true,paramType = "path"),
    })
    public Result delete(@PathVariable String id) {
         tweetService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}